<!doctype html>
<html>
	<head>
		<meta charset="utf-8">
		<title>后台</title>
		<link rel="stylesheet" href="/static/admin/css/style.css">
		<script src="/static/admin/js/ckeditor/ckeditor.js"></script>
	</head>
	<body>
		<div class="wrap wrap-article-edit">
			<h1>文章管理</h1>
			<div class="tips"></div>
			<div class="box">
				<div class="box-title">添加/编辑文章</div>
				<div class="box-body">
<!--					<form method="post" action="?id=0&token=d1d8da704cea56e9045df5ca761993c0" enctype="multipart/form-data">-->
						<table>
							<tr><th width="80">标题：</th><td><input type="text" name="title" value=""></td></tr>
							<tr><th>栏目：</th><td><select name="category_id">
								<option value="0">无</option>
								{foreach $list as $k=>$v}
										<option value="{$v.id}"  >{$v.name}</option>
											{if isset($v.child)}
											{foreach $v.child as $val}
											<option value="{$val.id}"  >—— {$val.name}</option>
											{/foreach}
											{/if}
								{/foreach}
									</select></td></tr>
							<tr class="s-author">
								<th>作者：</th>
								<td><input type="text" name="author" value="admin" disabled="disabled"></td>
							</tr>
							<tr class="s-keywords">
								<th>关键字：</th>
								<td><input type="text" name="keywords" value="">
									<span>多个关键字 请用英文逗号（,）分开</span>
								</td>
							</tr>
							<tr class="s-description">
								<th>内容提要：</th>
								<td><textarea name="description"></textarea><span>（内容提要请在 200 个字以内）</span></td>
							</tr>
		<tr class="s-thumb" >
			<th>封面图片：</th>
			<td>
				<img name="image" src=""  style="display:none">
			<input type="file" name="thumb" ><span>（超过 780*220 图片将被缩小）</span>
			</td>
		</tr>
		<tr class="s-editor">
			<th>编辑内容：</th>
			<td>
				<textarea name="content" ></textarea>
			</td>
		</tr>
		<tr class="s-act">
			<th></th>
			<td>
			<input type="submit" onclick="add(this)" value="立即发布" >
			<input type="submit" value="保存草稿" onclick="add(this)" name="save">
			</td>
		</tr>
	</table>
<!--	</form>-->
</div>
</div>
</div>
<script src="/static/admin/js/article.config.js"></script>
<script src="/static/admin/js/jquery.2.1.4.min.js"></script>
<script>
CKEDITOR.config.height = 400;
CKEDITOR.config.width = "100%";
let image = '';
let title = $("input[name=title]");//标题
let category_id = $("select[name=category_id]");//栏目
let author = $("input[name=author]");//作者
let keywords =  $("input[name=keywords]");//关键字
let description = $("textarea[name=description]");//内容摘要
let  content = CKEDITOR.replace("content");//编辑的内容
$('[name="thumb"]').change(function (){
	let formData = new FormData();
	formData.append('thumb',$("input[name=thumb]")[0].files[0]);//图片
	$.ajax({
		type:'POST',
		url:'upload',
		data:formData,
		processData:false,
		contentType:false,
		success:function (res){
			$('[name="image"]').attr('src',res).attr('style','display: show');
			image = res;
			console.log(res);
		},
		//图片上传错误提示
		error:function (request){
			console.log(request);
		}
	})
})

function add(save){
	save.value == '立即发布' ? save=1 : save=0;//发帖/草稿
	let formData = new FormData();
	formData.append('thumb',image);//图片
	formData.append('title',title.val());
	formData.append('category_id',category_id.val());
	formData.append('author',author.val());
	formData.append('keywords',keywords.val());
	formData.append('description',description.val());
	formData.append('content',content.getData());
	formData.append('save',rtrtrt);
	$.ajax({
		url:'cp_article_edit',
		type:'POST',
		data:formData,
		dataType:'json',
		processData:false,
		contentType:false,
		success:function (res){
			if(res.code== 0){
				alert(res.msg);
			}else{
				alert(res.msg);
				setTimeout(()=>{
					window.location.href ='http://www.cms.com/index.php/home/index/index';
				},100);
			}
		}
	})
}







</script>
</body>
</html>


